/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;
using System.Collections;

namespace EnvoxDecompiler
{
	/// <summary>
    /// Summary description for Block_SplitVariable.
	/// </summary>
	public class Block_SplitVariable : Block
	{

        private string _variableOrExpression;
        private string _delimiter;
        private int _adjustSourceForIteration;
        private int _clearPreviousContents;
        private ArrayList _destinationVariables = new ArrayList();

		public override TYPE GetBlockType()
		{
			return TYPE.SplitVariable;
		}

		public override string GetXML(int x, int y)
		{
            Trace.Debug("Block_SplitVariable::GetXML Started.");
            // <SplitVar name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<SplitVar name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
            //  ...description="DESC"...
            xml += GetDescriptionXML();
            //  ...clearDTMF="_clearDTMFBuffer"...
            xml += GetClearDTMFXML();
            // ...minUserLevel="_userLevel"
            xml += GetMinUserLevelXML();
            // ...cpMessage=""...
            xml += GetControlPanelMessageXML();

            // ...sourceType= type of source, variable or expression
            // If _adjustSourceForIteration then it MUST be a variable (that option is not enabled
            // unless it is a variable) else we put it as a expression which will work always.
            // TODO: Add something like "isAVariable(varname) to script scope so that we can 
            // know for real
            if (_adjustSourceForIteration == 1)
            {
                xml += " sourceType=\"0\"";
            }
            else 
            {
                xml += " sourceType=\"1\"";
            }
            // ...source="_variableOrExpression" >
            xml += " source=\"" + Utils.XmlString(_variableOrExpression) + "\"";
            // ...delimiter="_delimiter" >
            xml += " delimiter=\"" + Utils.XmlString(_delimiter) + "\"";
            // ...adjustSrc="_adjustSourceForIteration" >
            xml += " adjustSrc=\"" + _adjustSourceForIteration + "\"";
            // ...clearDest="_clearPreviousContents" >
            xml += " clearDest=\"" + _clearPreviousContents + "\">\r\n";
            
            // <Inputs>
            xml += GetInputsXML(_blockId);

            // <Outputs>
            xml += GetOutputsXML();

            // <Logs>
            xml += GetLogsXML();

            xml += GetHandledErrorsXML();

            // Destination variables
            xml += "\t\t\t\t<DestVars>\r\n";
            foreach(string destination in _destinationVariables)
            {
                xml += "\t\t\t\t\t<Element value=\"" + destination + "\"/>\r\n";
            }
            xml += "\t\t\t\t</DestVars>\r\n";

            // </SplitVar>
            xml += "\t\t\t</SplitVar>\r\n";
            return xml;
        }

		public Block_SplitVariable(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
            Trace.Debug("Block_SplitVariable created.");

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
            outputErrorIds.Add(new BlockOutput(OUTPUT.TooLong, 152));
            outputErrorIds.Add(new BlockOutput(OUTPUT.TooShort, 154));

			_currentPos += 12;

            // Adjust source for iteration
            _adjustSourceForIteration = rawData[_currentPos];

            _currentPos += 2;
            
            // Clear previous contents of destination variable
            _clearPreviousContents = rawData[_currentPos];

            _currentPos += 2;

			// source variable or expression
			_variableOrExpression = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// delimiter
            _delimiter = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

            // destination variables
            string tmp = ".";
            while (tmp != "")
            {
                tmp = Utils.DataToString(rawData, _currentPos);
                _currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
                if (tmp != "")
                {
                    _destinationVariables.Add(tmp);
                }
            }

            foreach (string destinationVar in _destinationVariables)
            {
                Trace.Debug("Destination: " + destinationVar);
            }

            Trace.Debug("SplitVariable: adjust: " + _adjustSourceForIteration +
                ", variableOrExpression: " + _variableOrExpression +
                ", delimiter: " + _delimiter +
                ", clearPreviousContents: " + _clearPreviousContents);
            
		}
	}
}
